package com.enjoy.concurrent.ch1.threadlocal;

import java.util.Arrays;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ThreadLocal造成的内存泄漏演示
 */
public class ThreadLocalOOM {
    public static final int TASK_LOOP_SIZE = 500;
    final static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>());
    static class LocalVariable{
        // 5m大小的数组
        private final byte[] a = new byte[1024 * 10124 * 50];

    }

    public static void main(String[] args) throws InterruptedException {

        /*5*5=25*/
        for (int i = 0; i < TASK_LOOP_SIZE; ++i) {
            poolExecutor.execute(() -> {
                //localVariable.set(new LocalVariable());
                new LocalVariable();
                System.out.println("use local variable");
                //localVariable.remove();
            });

            Thread.sleep(100);
        }
        System.out.println("pool execute over");
    }
}
